package controllers;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import play.data.binding.As;
import play.data.validation.Required;
import client.RestException;

import com.ineatconseil.yougo.dto.RequestDto;
import com.ineatconseil.yougo.dto.RequestStatusDto;
import com.ineatconseil.yougo.dto.RequestTypeDto;

public class NewRequest extends AbstractConroller {

	public static void index() throws RestException {
		List<RequestTypeDto> types = getRequestTypes();
		render(types);
	}

	public static void create(String observations, @Required Long requesttype, @Required @As("dd/MM/yyyy") Date start,
			@Required @As("dd/MM/yyyy") Date end) throws RestException, ParseException {

		if (!validation.hasErrors()) {

			if (!start.equals(end)) {
				validation.future(start);
				validation.future(end);
				validation.future(end, start);
			}

			if (!validation.hasErrors()) {
				Long userid = getUserid();
				RequestDto req = new RequestDto();
				req.setAskComment(observations);
				req.setFrom(start);
				req.setTo(end);
				req.setStatus(RequestStatusDto.PENDING);
				req.setTypeId(requesttype);
				client.postRequestDtos(userid, req);
			}
		}
		params.flash();
		validation.keep();
		cleanRequests();
		cleanDetailledUser();
		//render("Application/index.html");
		Application.index();
	}
}
